package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.model.product.BaseCategory1;
import com.atguigu.gmall.model.product.BaseCategoryView;
import com.atguigu.gmall.model.rpc.product.BaseCategory;
import com.atguigu.gmall.product.mapper.BaseCategory1Mapper;
import com.atguigu.gmall.product.service.BaseCategory1Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class BaseCategory1ServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategory1Service {

    @Resource
    BaseCategory1Mapper  baseCategory1Mapper;

    Map<String,List<BaseCategory>> categoryCache = new HashMap<>();

    @Override
    public List<BaseCategory> getAllCategoryForIndexPage() {

        //缓存的使用逻辑
        //1、先看缓存是否有
        List<BaseCategory> cacheData = categoryCache.get("getAllCategoryForIndexPage");
        if(cacheData == null){
            //缓存没菜单数据，查数据库
            List<BaseCategory> page = baseCategory1Mapper.getAllCategoryForIndexPage();
            categoryCache.put("getAllCategoryForIndexPage",page);
            return page;
        }else {
            return cacheData;
        }
    }

    @Override
    public BaseCategoryView getCategoryPathBySkuId(Long skuId) {

        return baseCategory1Mapper.getCategoryPathBySkuId(skuId);
    }
}
